package locacao.integracao.dao.pstPostgreSQL;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import locacao.integracao.dao.ServicoDAO;
import locacao.negocio.to.ServicoTO;

public class ServicoPgsqlDAO implements ServicoDAO {

    private ArrayList<ServicoTO> servicos;

    public ServicoPgsqlDAO(){
	   setServicos(new ArrayList<ServicoTO>());
    }


    public void addOrUpdateServico(ServicoTO servico) {
    	Connection conn = null;
    	CallableStatement spManterServico = null;

    	try {        	
          String sql = "{call locacao.manter_servico(?,?,?)}";
          conn = ConnectionRentACar.getInstance().getConnection();
          
          spManterServico = conn.prepareCall(sql);
          
          spManterServico.setInt(1, servico.getIdServico());
          spManterServico.setString(2, servico.getDescricaoServico());
          spManterServico.setInt(3, (int)servico.getValorUnitario());
          
          spManterServico.execute();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        } finally {
          try {
            if (spManterServico != null)
            	spManterServico.close();

          } catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
    }

    /**
     *

    /**
     * @param servico
     */
    public void excludeServico( ServicoTO servico )  {
    	Connection conn = null;
    	CallableStatement spDeleteServico = null;

    	try {        	
          String sql = "{call locacao.delete_servico(?)}";
          conn = ConnectionRentACar.getInstance().getConnection();
          
          spDeleteServico = conn.prepareCall(sql);
          
          spDeleteServico.setInt(1, servico.getIdServico());         
          
          spDeleteServico.execute();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        } finally {
          try {
            if (spDeleteServico != null)
            	spDeleteServico.close();

          } catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
    }


  
    /**
     * @return
     */
    public String[] findServicoById(Integer id){
    	 Connection conn = null;
    	 Statement stmt = null;
    	 ResultSet rSet = null;
    	 String[] info = null;

    	 String sql = "SELECT  idservico, descricao, vlrunit ";
    	 sql += " FROM locacao.servico WHERE idservico =  " + id;

    	 try {
    		 conn = ConnectionRentACar.getInstance().getConnection();
    		 stmt = conn.createStatement();
    		 rSet = stmt.executeQuery(sql);

    		 if (rSet != null) {    			 
    			 while (rSet.next()) {
    				 
    				info = new String[servicos.size()];
    	    		   info[0] = rSet.getInt("idservico") 
    	       		   + " - " + rSet.getString("descricao")
    	       		   + " - " + rSet.getFloat("vlrunit");    		   
    			 }    			 
    		 }
			 return info;
			 
    	 } catch (Exception e) {
    		 System.out.println(e.getMessage());
    		 return null;
    	 } finally {
    		 try {
    			 if (rSet != null)
    				 rSet.close();
    			 if (stmt != null)
    				 stmt.close();
    		 } catch (SQLException e) {
    			 System.out.println(e.getMessage());
    		 }
    	 }
    }

    
    /**
     * @return
     */
    public String[] getInfoServicos()
    {
        ServicoTO servico;
        servicos = getServicos();
        String[] info = new String[servicos.size()];
        Iterator iteratorServicos;
        iteratorServicos = servicos.iterator();
        int i=0;
        while ( iteratorServicos.hasNext() )
        {
            servico = (ServicoTO) iteratorServicos.next();
            info[i] = servico.getIdServico() 
            		+ " - " + servico.getDescricaoServico()   
            		+ " - " + servico.getValorUnitario();
            i++;
        }
        return info;
    }


    /**
     * @return
     */
    public ArrayList<ServicoTO> getServicos() {
    	 Connection conn = null;
    	 Statement stmt = null;
    	 ResultSet rSet = null;
    	 ServicoTO servico = null;

    	 String sql = "SELECT  idservico, descricao, vlrunit ";
    	 sql += " FROM locacao.servico ORDER BY idservico";

    	 try {
    		 conn = ConnectionRentACar.getInstance().getConnection();
    		 stmt = conn.createStatement();
    		 rSet = stmt.executeQuery(sql);

    		 if (rSet != null) {
    			 servicos = new ArrayList();
    			 while (rSet.next()) {
    				 
    				 servico = new ServicoTO();
    				 
    				 servico.setIdServico(rSet.getInt("idservico"));
    				 servico.setDescricaoServico(rSet.getString("descricao"));
    				 servico.setValorUnitario(rSet.getFloat("vlrunit"));
    				 
    				 servicos.add(servico);
    			 }
    		 }
    	 } catch (Exception e) {
    		 System.out.println(e.getMessage());
    	 } finally {
    		 try {
    			 if (rSet != null)
    				 rSet.close();
    			 if (stmt != null)
    				 stmt.close();
    		 } catch (SQLException e) {
    			 System.out.println(e.getMessage());
    		 }
    	 }
    	 return servicos;
    }

    private void setServicos(ArrayList<ServicoTO> servicos) {
    	this.servicos = servicos;
    }
    
}
